package tapioca.testing;

import java.util.Collection;
import java.util.Collections;

import org.easymock.EasyMock;
import org.easymock.IArgumentMatcher;

public class Matchers {

  private Matchers() {
    // Utility classes should have private constructors.
  }
  
  public static class CollectionMatcher<T> implements IArgumentMatcher {

    private final Collection<T> expected;
    
    public CollectionMatcher(Collection<T> expected) {
      this.expected = expected;
    }
    
    @Override
    public boolean matches(Object obj) {
      if (!(obj instanceof Collection)) {
        return false;
      }
      
      Collection<?> actual = (Collection<?>) obj;
      
      if (expected.size() != actual.size()) {
        return false;
      }
      
      for (T toMatch : expected) {
        if (!actual.contains(toMatch)) {
          return false;
        }
      }
      
      return true;
    }
    
    @Override
    public void appendTo(StringBuffer buffer) {
      buffer.append("Expected collection " + expected);
    }
    
    public static <E> Collection<E> eqCollection(Collection<E> expected) {
      EasyMock.reportMatcher(new CollectionMatcher<E>(expected));
      return Collections.emptyList();
    }    
  }    
  
}
